package com.link.base.base.product.service;

import com.link.base.base.product.model.ApiPriceList;
import com.link.base.base.product.model.PriceList;
import com.link.base.base.product.model.PriceListItem;
import com.link.base.core.apiutils.ApiBeanUtils;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/12 16:18
 * @version 1.0
 */

@Service
public class ApiPriceListServiceImpl implements ApiPriceListService {

    @Resource
    private PriceListService priceListService;
    @Resource
    private PriceListItemService priceListItemService;

    @Transactional(rollbackFor = BasicServiceException.class)
    public Long upsertHeadAndItem(ApiPriceList apiPriceList) throws Exception {
        Long crmId = null;
        if (StringUtils.isBlank(apiPriceList.getIntegrationId())) {
            throw new ServiceException("PUBLIC-036");
        }
        PriceList example = new PriceList();
        example.setIntegrationId(apiPriceList.getIntegrationId());
        example.setPageFlag(false);
        List<PriceList> list = priceListService.queryByExamplePage(example);
        if (list.size() > 1) {
            throw new ServiceException("PRODUCT-001",apiPriceList.getIntegrationId());
        }

        if (list.size() == 0) {
            //头信息尚未新建，则全部数据新建
            PriceList priceList = new PriceList();
            ApiBeanUtils.copyProperties(apiPriceList, priceList);
            priceList.setId(null);
            priceListService.insert(priceList);
            List<PriceListItem> itemList = apiPriceList.getPriceItemList();
            if (itemList != null) {
                for (PriceListItem item : itemList) {
                    item.setHeadId(priceList.getId());
                    priceListItemService.insert(item);
                }
            }
            crmId = priceList.getId();
        } else if (list.size() == 1) {
            PriceList priceList = list.get(0);

            Long priceListId = priceList.getId();
            ApiBeanUtils.copyProperties(apiPriceList, priceList);
            priceList.setId(priceListId);
            priceListService.update(priceList);
            List<PriceListItem> itemList = apiPriceList.getPriceItemList();
            if (itemList != null) {
                PriceListItem itemExample = new PriceListItem();
                itemExample.setPageFlag(false);
                for (int i = 0; i < itemList.size(); i++) {
                    PriceListItem item = itemList.get(i);
                    String itemIntegrationId = item.getIntegrationId();
                    if (StringUtils.isBlank(itemIntegrationId)) {
                        throw new ServiceException("PROFUCT-002",String.valueOf(i));
                    }
                    itemExample.setIntegrationId(item.getIntegrationId());
                    List<PriceListItem> queryItemList = priceListItemService.queryByExamplePage(itemExample);
                    if (queryItemList.size() > 1) {
                        throw new ServiceException("PRODUCT-003",String.valueOf(i),itemIntegrationId);
                    } else if (queryItemList.size() == 0) {
                        item.setHeadId(priceList.getId());
                        item.setId(null);
                        priceListItemService.insert(item);
                    } else if (queryItemList.size() == 1) {
                        PriceListItem queryItem = queryItemList.get(0);
                        if (!priceList.getId().equals(queryItem.getHeadId())) {
                            throw new ServiceException("PRODUCT-004",String.valueOf(i),itemIntegrationId);
                        }
                        item.setHeadId(queryItem.getHeadId());
                        item.setId(queryItem.getId());
                        ApiBeanUtils.copyProperties(item, queryItem);
                        priceListItemService.update(item);
                    }
                }
            }
            crmId = priceList.getId();
        }
        return crmId;
    }




}
